package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.service.WorkPlanService;
import cn.school.workflow.vo.WorkPlanPersonVO;
import cn.school.workflow.vo.WorkPlanVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 工作计划
 */
@RestController
@RequestMapping("/workPlan")
@Api(tags = "工作计划接口")
public class WorkPlanController {

    @Resource
    private WorkPlanService workPlanService;

    /**
     * 查询流程部署信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findWorkPlanPageList")
    @ApiOperation("list => 分页查询工作计划")
    public CommonResult<CommonPage<WorkPlanVO>> findWorkPlanPageList(@RequestBody WorkPlanVO vo) throws ApiException{
        IPage<WorkPlanVO> pageList =  workPlanService.findWorkPlanPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }


    @PostMapping("/addOrUpdateWorkPlan")
    @ApiOperation("create => 创建违纪流程")
    public CommonResult<Integer> addOrUpdateWorkPlan(@RequestBody WorkPlanVO  vo) throws ApiException{
        if(vo.getWorkPlanPersonList()==null || vo.getWorkPlanPersonList().size()==0){
            return CommonResult.failed("缺少执行人");
        }
        int flag =  workPlanService.addOrUpdateWorkPlan(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("提交失败");
    }

    @PostMapping("/deleteWorkPlanById")
    @ApiOperation("delete => 删除工作计划")
    public CommonResult<WorkPlanVO> deleteWorkPlanById(@RequestBody WorkPlanVO  vo) throws ApiException{
        int num = workPlanService.deleteWorkPlanById(vo.getId());
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }

    @PostMapping("/findPersonPlanListByPlanId")
    @ApiOperation("query person => 查看工作计划执行人列表")
    public CommonResult<CommonPage<WorkPlanPersonVO>> findPersonPlanListByPlanId(@RequestBody WorkPlanPersonVO  vo) throws ApiException{
        IPage<WorkPlanPersonVO> list = workPlanService.findPersonPlanListByPlanId(vo);

        return  CommonResult.success(CommonPage.restPage(list));
    }

    @PostMapping("/findPersonPlanPageList")
    @ApiOperation("query safe => 查看个人工作计划列表")
    public CommonResult<CommonPage<WorkPlanPersonVO>> findPersonPlanPageList(@RequestBody WorkPlanPersonVO  vo) throws ApiException{
        vo.setUserName(SysUserUtils.getSysUser().getUsername());
        IPage<WorkPlanPersonVO> list = workPlanService.findPersonPlanListByPlanId(vo);
        return  CommonResult.success(CommonPage.restPage(list));
    }

    @PostMapping("/updateMySafePlan")
    @ApiOperation("query safe => 修改计划进度")
    public CommonResult<Integer> updateMySafePlan(@RequestBody WorkPlanPersonVO  vo) throws ApiException{
        int num = workPlanService.updateMySafePlan(vo);
        if(num>0) return CommonResult.success();
        return  CommonResult.failed();
    }

}
